package com.zdx.yswl.ui.activity;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.loopj.android.image.SmartImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.umeng.analytics.MobclickAgent;
import com.zdx.yswl.R;
import com.zdx.yswl.tool.KNetTool;
import com.zdx.yswl.ui.adapter.LpInfoAdapter;
import com.zdx.yswl.ui.base.BaseActivity;
import com.zdx.yswl.ui.bean.HeadImgBean;
import com.zdx.yswl.ui.listener.AnimateFirstDisplayListener;
import com.zdx.yswl.view.listview.BaseListView;

public class MainHouseTypeActivity extends BaseActivity implements OnClickListener {

	protected static final String TAG = MainHouseTypeActivity.class.getSimpleName();

	LpInfoAdapter adapter;
	BaseListView listView;
	ArrayList<HeadImgBean> mHoustypePictures;
	TextView temPosition;
	com.zdx.yswl.view.PhotoView imgView;
	TextView introduce;
	int temp = 0;
	private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	protected DisplayImageOptions options;


	public static Intent actionStart(Context context, ArrayList<HeadImgBean> houstypePicture) {
		Intent intent = new Intent(context, MainHouseTypeActivity.class);
		intent.putExtra("list", houstypePicture);
		return intent;
	}

	@Override
	protected void initSettings() {

	}

	@Override
	protected void initUi() {

	}

	@Override
	protected void saveSettings() {

	}

	@Override
	protected void freeMe() {

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_housetype);

		Intent mIntent = getIntent();
		mHoustypePictures = (ArrayList<HeadImgBean>) mIntent.getSerializableExtra("list");

		initNav();
		initData();
		initImagelord();
	}

	private void initImagelord() {
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.aio_image_default_round)
		.showImageForEmptyUri(R.drawable.aio_image_default_round)
		.showImageOnFail(R.drawable.aio_image_fail_round)
		 .considerExifParams(true)
		.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
		.cacheInMemory(true)
		.cacheOnDisc(true)//设置下载的图片是否缓存在SD卡中  
		.considerExifParams(true)
		.imageScaleType(ImageScaleType.EXACTLY)
//		.displayer(new RoundedBitmapDisplayer(20))//设置图片圆角
		.build();		
	}

	private void initNav() {
		setHeader("主力户型");
	}

	private void initData() {
		Button collectBtn = (Button) findViewById(R.id.collect_btn);
		ImageButton leftBtn = (ImageButton) findViewById(R.id.housetype_left_btn);
		ImageButton rightBtn = (ImageButton) findViewById(R.id.housetype_right_btn);
		leftBtn.setOnClickListener(this);
		rightBtn.setOnClickListener(this);
		collectBtn.setOnClickListener(this);

		temPosition = (TextView) findViewById(R.id.houstype_position_tv);
		imgView = (com.zdx.yswl.view.PhotoView) findViewById(R.id.housetype_img);
		introduce = (TextView) findViewById(R.id.housetype_picinfo_tv);

		setData(0);
	}

	/** 友盟session的统计 */
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	/** 友盟session的统计 */
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.housetype_left_btn) {
			temp--;
			setData(temp);
		} else if (v.getId() == R.id.housetype_right_btn) {
			temp++;
			setData(temp);
		} else if (v.getId() == R.id.collect_btn) {
			// TODO 收藏
		}

	}

	private void setData(int position) {
		while (position < 0) {
			position = position + mHoustypePictures.size();
		}

		/** imageloader 加载 */
		imageLoader.displayImage(mHoustypePictures.get(position % mHoustypePictures.size()).url,//
				     			imgView, options, animateFirstListener);
		introduce.setText(mHoustypePictures.get(position % mHoustypePictures.size()).label);
		temPosition.setText((position % mHoustypePictures.size()) + 1 + "/" + mHoustypePictures.size());
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
		}
		return true;
	}

	@Override
	public void onNotify(int what, int arg1, int arg2, Object obj) {

	}

	@Override
	public void onHandler(int what, int arg1, int arg2, Object obj) {

	}
}
